// pages/settlement/settlement.js
const utils = require('../../utils/util')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        title: "收款时间",
        _num: "1",
        list: [],
        page: 1,
        status: 'F',
        taskNumber: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        this.getList();
        this.getTaskNumber();
    },
//获取各个状态下的任务数量
    getTaskNumber: function () {
        utils.FetchRequest('/v1/api/task/count_task_by_status', {}, 'get').then((res) => {
            if (res.data.status == 200) {
                if (res.data.data.length > 4) {
                    let num = [];
                    let s = ["E", "F"];
                    let a = res.data.data;
                    for (let i = 0; i < s.length; i++) {
                        for (let b = 0; b < a.length; b++) {
                            if (a[b].status == s[i]) {
                                num.push(a[b]);
                            }
                        }
                    }
                    this.setData({
                        taskNumber: num
                    });
                }
            } else {
                wx.showToast({
                    title: res.data.message,
                    icon: 'none',
                    duration: 2000
                });
            }
        })
    },
    togger: function (e) {
        var index = e.currentTarget.dataset.index;
        this.setData({
            _num: index
        })
        if (this.data._num == "0") {
            this.setData({
                title: "验收时间",
                status: "E"
            })
        }
        if (this.data._num == "1") {
            this.setData({
                title: "收款时间",
                status: "F"
            })
        }
        this.setData({
            page: 1
        });
        this.getList();
        this.getTaskNumber();
    },
    //下拉刷新
    onPullDownRefresh: function () {
        // 显示顶部刷新图标
        wx.showNavigationBarLoading();
        this.setData({
            page: 1
        });
        this.getList();
        this.getTaskNumber();
    },
    //上拉加载
    onReachBottom: function () {
        var that = this;
        // 显示加载图标
        wx.showLoading({
            title: '正在加载中',
            duration: 1000
        });
        // 页数+1
        that.setData({
            page: that.data.page + 1
        });
        utils.FetchRequest(
            '/v1/api/task/list?status=' + that.data.status + '&pageNo=' + that.data.page + '&pageSize=10',
            {}, 'get').then((res) => {
            if (res.data.status == 200) {
                if (res.data.data.length != 0) {
                    var listTwo = res.data.data;
                    that.setData({
                        list: that.data.list.concat(listTwo)
                    })
                } else {
                    wx.showToast({
                        title: '没有更多了',
                        icon: 'none',
                        duration: 2000
                    });
                }
                wx.hideLoading();
            } else {
                wx.showToast({
                    title: res.data.message,
                    icon: 'none',
                    duration: 2000
                });
            }
        });
    },
    seeDetail: function (e) {
        var taskId = e.currentTarget.dataset.taskid;
        if (this.data._num == "0") {
            wx.navigateTo({
                url: '/pages/payment/payment?taskId=' + taskId
            })
        }
        if (this.data._num == "1") {
            wx.navigateTo({
                url: '/pages/completed/completed?taskId=' + taskId
            })
        }
    },
    getList: function () {
        wx.showLoading({
            title: '正在加载中！',
        });
        utils.FetchRequest(
            '/v1/api/task/list?status=' + this.data.status + '&pageNo=' + this.data.page + '&pageSize=10',
            {}, 'get').then((res) => {
            if (res.data.status == 200) {
                this.setData({
                    list: res.data.data
                })
                // 隐藏导航栏加载框
                wx.hideNavigationBarLoading();
                // 停止下拉动作
                wx.stopPullDownRefresh();
                wx.hideLoading();
            } else {
                wx.showToast({
                    title: res.data.message,
                    icon: 'none',
                    duration: 2000
                });
                wx.hideLoading();
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    }
})